/*
 *    Copyright (c) 2006 ThoughtWorks, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.thoughtworks.turtlemock.internal;

import com.thoughtworks.turtlemock.constraint.Constraint;

public class PairInvokeMethodResultLog extends BaseInvokeMethodResultLog {
    private final InvokeMethodResultLog bottom;
    private final InvokeMethodResultLog top;
    private final PairRollbackableInvokeLog pairExpection;

    public PairInvokeMethodResultLog(InvokeMethodResultLog bottom, InvokeMethodResultLog top) {
        super(bottom.getOperationName());
        this.bottom = bottom;
        this.top = top;
        pairExpection = new PairRollbackableInvokeLog(bottom, top);
    }

    public InvokeWithArgsResultLog withArgsConstraint(Constraint[] constraints) {
        if (bottom.timesNotAsserted() == 0) {
            return top.withArgsConstraint(constraints);
        }
        if (top.timesNotAsserted() == 0) {
            return bottom.withArgsConstraint(constraints);
        }
        InvokeWithArgsResultLog bottomTimesExpection = bottom.withArgsConstraint(constraints);
        InvokeWithArgsResultLog topTimesExpection = top.withArgsConstraint(constraints);
        return new PairInvokeWithArgsResultLog(bottomTimesExpection, topTimesExpection);
    }

    public InvokeWithArgsResultLog withAllArgsConstraint(Constraint constraint) {
        if (bottom.timesNotAsserted() == 0) {
            return top.withAllArgsConstraint(constraint);
        }
        if (top.timesNotAsserted() == 0) {
            return bottom.withAllArgsConstraint(constraint);
        }
        InvokeWithArgsResultLog bottomTimesExpection = bottom.withAllArgsConstraint(constraint);
        InvokeWithArgsResultLog topTimesExpection = top.withAllArgsConstraint(constraint);
        return new PairInvokeWithArgsResultLog(bottomTimesExpection, topTimesExpection);
    }

    public InvokeWithArgsResultLog withArgConstraint(Constraint constraint, int paramIndex) {
        if (bottom.timesNotAsserted() == 0) {
            return top.withArgConstraint(constraint, paramIndex);
        }
        if (top.timesNotAsserted() == 0) {
            return bottom.withArgConstraint(constraint, paramIndex);
        }
        InvokeWithArgsResultLog bottomTimesExpection = bottom.withArgConstraint(constraint, paramIndex);
        InvokeWithArgsResultLog topTimesExpection = top.withArgConstraint(constraint, paramIndex);
        return new PairInvokeWithArgsResultLog(bottomTimesExpection, topTimesExpection);
    }

    public InvokeWithArgsResultLog noArgs() {
        if (bottom.timesNotAsserted() == 0) {
            return top.noArgs();
        }
        if (top.timesNotAsserted() == 0) {
            return bottom.noArgs();
        }
        InvokeWithArgsResultLog bottomTimesExpection = bottom.noArgs();
        InvokeWithArgsResultLog topTimesExpection = top.noArgs();
        return new PairInvokeWithArgsResultLog(bottomTimesExpection, topTimesExpection);
    }

    public void times(int times) {
        pairExpection.times(times);
    }

    public int timesNotAsserted() {
        return pairExpection.timesNotAsserted();
    }

    public void rollback() {
        pairExpection.rollback();
    }

}
